﻿$(function () {
    $("#signupsubmit").click(function () {
        SubmitSignup();
    });

    $("#signupbody").keypress(function (event) {
        if (event.which == '13') {
            SubmitSignup();
        }
    });

    $("#signupclose").click(function () {
        hideModal($("#signupbody"));
        $("#signupbody").remove();
    });
});

function SubmitSignup() {

    $("#signupsubmit").hide();

    var input = {
        FirstName: $("#firstname").val(),
        LastName: $("#lastname").val(),
        Email: $("#signupemail").val(),
        Password1: $("#signuppassword1").val(),
        Password2: $("#signuppassword2").val()
    };

    Ajax({
        url: "/Security/TrySignup",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == true) {
                $("#content").load("/Security/SignupComplete", input);
            } else {
                SignupValidationErrors(data);
            }
        },
        complete: function () {
            $("#signupsubmit").show();
        }
    });
}

function SignupValidationErrors(data) {
    var msg = "";
    $("#firstnameval").remove();
    $("#lastnameval").remove();
    $("#signupemailval").remove();
    $("#signuppassword1val").remove();
    $("#signuppassword2val").remove();

    if (data.IsInvalidInput) {
        if (data.FirstNameValidationCode > 0) {
            if (data.FirstNameValidationCode == 1) {
                msg = "Please enter your first name";
            } else if (data.FirstNameValidationCode == 2) {
                msg = "name cannot be more than 50 characters";
            }
            $("#firstname").focus();
            $("#firstname").after('<span id="firstnameval" class="input-notification error png_bg">' + msg + '</span>');
        }

        if (data.LastNameValidationCode > 0) {
            if (data.LastNameValidationCode == 1) {
                msg = "Please enter your last name";
            } else if (data.LastNameValidationCode == 2) {
                msg = "name cannot be more than 50 characters";
            }
            $("#lastname").focus();
            $("#lastname").after('<span id="lastnameval" class="input-notification error png_bg">' + msg + '</span>');
        }

        if (data.EmailValidationCode > 0) {
            if (data.EmailValidationCode == 1) {
                msg = "Please enter your email";
            } else if (data.EmailValidationCode == 2) {
                msg = "Email cannot be more than 50 characters";
            } else if (data.EmailValidationCode == 3) {
                msg = "Please enter a valid email";
            }
            $("#signupemail").focus();
            $("#signupemail").after('<span id="signupemailval" class="input-notification error png_bg">' + msg + '</span>');
        }

        if (data.Password1ValidationCode > 0) {
            if (data.Password1ValidationCode == 1) {
                msg = "Please enter password";
            } else if (data.Password1ValidationCode == 2) {
                msg = "password cannot be more than 50 characters";
            }
            $("#signuppassword1").focus();
            $("#signuppassword1").after('<span id="signuppassword1val" class="input-notification error png_bg">' + msg + '</span>');
        }

        if (data.Password2ValidationCode > 0) {
            if (data.Password2ValidationCode == 1) {
                msg = "Please confirm password";
            } else if (data.Password2ValidationCode == 2) {
                msg = "password cannot be more than 50 characters";
            } else if (data.Password2ValidationCode == 3) {
                msg = "passwords do not match";
            }
            $("#signuppassword2").focus();
            $("#signuppassword2").after('<span id="signuppassword2val" class="input-notification error png_bg">' + msg + '</span>');
        }

    } else if (data.EmailTaken) {
        msg = "Email already registered";
        $("#signupemail").focus();
        $("#signupemail").after('<span id="signupemailval" class="input-notification error png_bg">' + msg + '</span>');
    } 
}